package com.kadron.creator;

import com.kadron.application.KApplicationModel;
import com.kadron.dao.KApplicationDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.datamodel.KModelCreator;
import com.kadron.entity.Application;
import com.kadron.entity.KApplicationImage;

public class KApplicationModelCreator implements KModelCreator {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5431750255930389850L;

	private KImageDAO imageDAO;
	private KApplicationDAO applicationDAO;
	
	public KApplicationModelCreator(KImageDAO imageDAO, KApplicationDAO applicationDAO) {
		super();
		this.imageDAO = imageDAO;
		this.applicationDAO = applicationDAO;
	}

	@Override
	public KApplicationModel create(long id) {
		Application application = (Application) applicationDAO.find(id, Application.class);
		KApplicationImage img = imageDAO.getApplicationImage(application.getId());
		if (img == null) {
			img = new KApplicationImage(application.getId());
		}
		KApplicationModel model = new KApplicationModel(application,img);
		return model;
	}
	
	public KApplicationModel getNew() {
		Application application = applicationDAO.getNew();
		if (application == null) {
			return null;
		}
		KApplicationImage img = imageDAO.getApplicationImage(application.getId());
		if (img == null) {
			img = new KApplicationImage(application.getId());
		}
		KApplicationModel model = new KApplicationModel(application,img);
		return model;
	}

}
